package com.qiankan.qmsj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.entity.Result;
import com.qiankan.qmsj.net.APIHub;
import com.qiankan.qmsj.utils.CountDownTimerUtils;
import com.qiankan.qmsj.utils.StringUtils;
import com.qiankan.qmsj.widget.TipToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SettingPasswordActivity extends BaseActivity {
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_confirm)
    EditText mEtConfrim;
    @BindView(R.id.et_verifyCode)
    EditText mEtVerifyCode;
    @BindView(R.id.tv_getVerifyCode)
    TextView mTvVerifyCode;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    private String password;
    private String passwordConfirm;
    private String verifyCode;
    private UserInfo userInfo;
    private CountDownTimerUtils mCountDownTimerUtils;

    public static Intent newIntent(Context context) {
        return new Intent(context, SettingPasswordActivity.class);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting_password;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        mCountDownTimerUtils = new CountDownTimerUtils(mTvVerifyCode, 60000, 1000);
    }

    @OnClick(R.id.tv_getVerifyCode)
    public void onClickVerifyCode(View view) {
        mCountDownTimerUtils.start();
        Map<String, String> param = new HashMap<String, String>();
        userInfo = UserDBHelper.getInstance(this).getUserInfo();
        param.put("type", "1");
        param.put("phone", userInfo.getPhone());
        APIHub.getsInstance().getBaseService().getValidate(param).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Result result = response.body();
                if (result.getCode().equals("200")) {
                    TipToast.toast(SettingPasswordActivity.this, "获取验证码成功！");
                } else {
                    mCountDownTimerUtils.cancel();
                    mCountDownTimerUtils.onFinish();
                    TipToast.toast(SettingPasswordActivity.this, result.getRemark());
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                mCountDownTimerUtils.cancel();
                mCountDownTimerUtils.onFinish();
                TipToast.toast(SettingPasswordActivity.this, "网络连接失败");
            }
        });
    }

    @OnClick(R.id.btn_confirm)
    public void onClickConfirm(View view) {
        userInfo = UserDBHelper.getInstance(this).getUserInfo();
        password = mEtPassword.getText().toString();
        passwordConfirm = mEtConfrim.getText().toString();
        verifyCode = mEtVerifyCode.getText().toString();
        if (TextUtils.isEmpty(password)) {
            TipToast.toast(this, "请输入密码");
            return;
        }
        if (!StringUtils.isPassword(password)) {
            TipToast.toast(this, "请输入6-20位字母数字密码组合");
            return;
        }
        if (TextUtils.isEmpty(passwordConfirm)) {
            TipToast.toast(this, "请再次确认密码");
            return;
        }
        if (!StringUtils.isPassword(passwordConfirm)) {
            TipToast.toast(this, "请输入6-20位字母数字密码组合");
            return;
        }
        if (!password.equals(passwordConfirm)) {
            TipToast.toast(this, "两次输入的密码不一致");
            return;
        }
        if (TextUtils.isEmpty(verifyCode)) {
            TipToast.toast(this, "请输入验证码");
            return;
        }
        Map<String, String> param = new HashMap<String, String>();
        param.put("id", userInfo.getId());
        param.put("phone", userInfo.getPhone());
        param.put("password", password);
        param.put("validate", verifyCode);
        showLoadingDialog();
        APIHub.getsInstance().getBaseService().updateUserInfo(param).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                dismissLoadingDialog();
                Result result = response.body();
                if (result.getCode().equals("200")) {
                    TipToast.toast(SettingPasswordActivity.this, "修改成功！");
                    finish();
                } else {
                    TipToast.toast(SettingPasswordActivity.this, result.getRemark());
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                dismissLoadingDialog();
                TipToast.toast(SettingPasswordActivity.this, "网络连接失败");
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onClickBack(View view) {
        finish();
    }
}
